﻿using System;
using UnityEngine;

namespace Framework
{

    /// <summary>
    /// 基础对象，仅支持释放和创建
    /// </summary>
    public abstract class ObjectBase : IReference
    {
        private string m_Name;
        private object m_Target;
        /// <summary>
        /// 上次使用的时间
        /// </summary>
        private DateTime m_LastUseTime;
        
        /// <summary>
        /// 初始化对象基类的新实例。
        /// </summary>
        public ObjectBase()
        {
            m_Name = null;
            m_LastUseTime = default(DateTime);;
        }

        public DateTime LastUsedTime
        {
            get => m_LastUseTime;
            set => m_LastUseTime = value;
        }
        /// <summary>
        /// 获取对象名称。
        /// </summary>
        public string Name
        {
            get
            {
                return m_Name;
            }
        }

        /// <summary>
        /// 获取对象。
        /// </summary>
        public object Target
        {
            get
            {
                return m_Target;
            }
        }
        
        protected virtual void Init(string name, object target)
        {
            if (target == null)
            {
                throw new GameFrameworkException(string.Format("Target '{0}' is invalid.", name));
            }

            m_Name = name ?? string.Empty;
            m_Target = target;
            m_LastUseTime = DateTime.UtcNow;
            ;
        }
        
        /// <summary>
        /// 释放对象。
        /// </summary>
        /// <param name="isShutdown">是否是关闭对象池时触发。</param>
        protected internal abstract void Release(bool isShutdown);

        public virtual void Clear()
        {
            m_Name = null;
            m_Target = null;
            m_LastUseTime = default(DateTime);;
        }
    }



}